package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.atguigu.lease.web.app.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserInfoService userInfoService;





    @Override
    public String login(LoginVo loginVo) {
        //判断手机号是否为空
        if (!StringUtils.hasText(loginVo.getPhone())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }
        //判断短信验证码是否为空
        if (!StringUtils.hasText(loginVo.getCode())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        //从redis中根据 手机号 查询 value 验证码是否为空 如果是空抛异常验证码过期了
        String verfy = redisTemplate.opsForValue().get(RedisConstant.APP_LOGIN_PREFIX+loginVo.getPhone()).toString();
        if (!StringUtils.hasText(verfy)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }
        //从redis中 验证  验证码是否正确 不正确就抛异常
        String phoneNum =loginVo.getCode();
        if (!verfy.equals(phoneNum)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }
        //去数据库中查询 该手机号的用户是否存在 如果不存在 则创建新用户
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserInfo::getPhone,loginVo.getPhone());
        UserInfo userInfo = userInfoService.getOne(lambdaQueryWrapper);
        if (userInfo==null){
            //如果没查到该用户信息 则创建新用户\
            userInfo = new UserInfo();
            userInfo.setPhone(loginVo.getPhone());
            userInfo.setStatus(BaseStatus.ENABLE);
            userInfoService.save(userInfo);
        }
        //判断 该用户是否被禁
        if (userInfo.getStatus()==BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
        }

        String token = JwtUtil.createToken(userInfo.getId(),loginVo.getPhone());


        //



        return token;
    }

    @Override
    public UserInfoVo getUserInfoVo(Long userId) {
        UserInfoVo userInfoVo = new UserInfoVo();

        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserInfo::getId,userId);
        UserInfo userInfo = userInfoService.getOne(lambdaQueryWrapper);

        if(!StringUtils.hasText(userInfo.getNickname())){
            userInfoVo.setNickname(userInfo.getPhone());
        }
        userInfoVo.setAvatarUrl(userInfo.getAvatarUrl());

        return userInfoVo;
    }
}
